import random
from typing import List

def generate_random_case(min_length: int = 1, max_length: int = 100) -> List[int]:
    """生成一个随机测试用例"""
    length = random.randint(min_length, max_length)
    arr = list(range(1, length + 1))
    random.shuffle(arr)
    return arr

def generate_sorted_case(length: int) -> List[int]:
    """生成已排序的测试用例"""
    return list(range(1, length + 1))

def generate_reverse_sorted_case(length: int) -> List[int]:
    """生成逆序的测试用例"""
    return list(range(length, 0, -1))

def generate_test_cases(num_cases: int = 10) -> List[List[int]]:
    """生成一组测试用例"""
    test_cases = []
    
    # 添加一些边界情况
    test_cases.append([])  # 空数组
    test_cases.append([1])  # 单元素
    test_cases.append([1, 2])  # 已排序
    test_cases.append([2, 1])  # 逆序
    
    # 随机生成一些测试用例
    for _ in range(num_cases - 4):
        length = random.randint(1, 100)
        if random.random() < 0.3:  # 30%概率生成已排序或逆序
            if random.random() < 0.5:
                test_cases.append(generate_sorted_case(length))
            else:
                test_cases.append(generate_reverse_sorted_case(length))
        else:
            test_cases.append(generate_random_case(1, length))
    
    return test_cases

def save_test_cases(test_cases: List[List[int]], filename: str = "test_cases.txt"):
    """将测试用例保存到文件"""
    with open(filename, 'w') as f:
        for case in test_cases:
            f.write(','.join(map(str, case)) + '\n')

def load_test_cases(filename: str = "test_cases.txt") -> List[List[int]]:
    """从文件加载测试用例"""
    test_cases = []
    with open(filename, 'r') as f:
        for line in f:
            if line.strip():
                test_cases.append(list(map(int, line.strip().split(','))))
    return test_cases

if __name__ == "__main__":
    # 生成并保存测试用例
    test_cases = generate_test_cases(20)
    save_test_cases(test_cases)
    print(f"Generated {len(test_cases)} test cases and saved to test_cases.txt")